/* Copyright Andy Mitchell 2007. No warranties */

function LabelSelectUI(gtdinboxPage, elParent, createWithAjax, threadActions ){
	this.gtdinboxPage = gtdinboxPage;
	this.doc = gtdinboxPage.doc;
	this.settings = gtdinboxPage.settings;
	this._labels = gtdinboxPage._labels;
	this.eventObservers = new EventObservers(this);
	this.eventHandlers = new EventHandlers_Mutator(this);
	this._showHiders = [];
	this._optionsShortList = {maxSelected:100, maxBody:100, maxRecentByName:3, maxRecent:3};
	this._createWithAjax = createWithAjax;
	this._threadActions = threadActions;
	
	this._elLabelHash = {};
	this._labelActiveHash = {};
	this._elContainers = {};
	this._types = [];
	for( t in this.settings.labels.types ){
		if( t!="Old" ) this._types.push(t);
	}
	//this._types.reverse();

	this.create(elParent);
}

LabelSelectUI.prototype.create = function(elParent){

	this.el = this.doc.createElement("DIV");
		this.el.className = "label-selector";
		//this.el.style.width = "560px";
	if( elParent ) elParent.appendChild( this.el );
		
	for( var i = 0; i < this._types.length; i++ ){
		var t = this._types[i];
		var elContainer = this.doc.createElement("DIV");
			elContainer.className = "container";
		this.el.appendChild( elContainer );
				
		if( t=="Projects" ){	// create inner containers
			var elContainer0 = this.doc.createElement("DIV");
			elContainer.appendChild(elContainer0);
			var elContainer1 = this.doc.createElement("DIV");
			elContainer.appendChild(elContainer1);
			var elContainer2 = this.doc.createElement("DIV");
			elContainer.appendChild(elContainer2);
	
			this._elContainers["RecentProjects"] = elContainer0;
			this._elContainers["Projects"] = elContainer1;
			this._elContainers["Old"] = elContainer2;
		}else{
			this._elContainers[t] = elContainer;
		}
	}
	var elClear = this.doc.createElement("DIV");
		elClear.style.clear = "both";
	this.el.appendChild(elClear);
	
	this.elNew = this.doc.createElement("DIV");
		this.elNew.className = "new-labels-list";
		this.elNew.style.display = "none";
	this.el.appendChild( this.elNew );
	
	this.elActionsContainer = this.doc.createElement("DIV");
		this.elActionsContainer.className = "actions";
	this.el.appendChild( this.elActionsContainer );
	var elCreate = this.doc.createElement("BUTTON");
		elCreate.innerHTML = "New Label";
		elCreate.className = "create-label";
		this.eventObservers.observe(elCreate, "click", this.clickCreate, false);
	this.elActionsContainer.appendChild( elCreate );
	var elClear = this.doc.createElement("DIV");
		elClear.style.clear = "both";
	this.el.appendChild( elClear );
	
	this.eventObservers.observe( this.el, "mousedown", this.clickLabel, false);
};

LabelSelectUI.prototype.refresh = function(thread){
	try{
		if( thread ){
			this.thread = thread;
			this._optionsShortList.thread = thread;
		}
		if( this._lastRefresh && this._labels._lastRefresh && this._labels._lastRefresh<=this._lastRefresh ){
			// No change to labels. Just remove any selections.
			var elLabels = this._labels.getLabelsElForProjectShortList(this._optionsShortList);
			this.refresh_addElLabels("RecentProjects", elLabels);
			if( this.thread ) this.selected( this.thread.labels );
			return;
		}
		this._lastRefresh = (new Date()).valueOf();
		
		this._elLabelHash = {};
		var labelname = "";
				
		for( t in this._elContainers ){
			var elLabels = (t=="RecentProjects")? this._labels.getLabelsElForProjectShortList(this._optionsShortList) : this._labels.getLabelsEl(t);
			if( elLabels ) this.refresh_addElLabels(t, elLabels);
		}
		if( this.thread ) this.selected( this.thread.labels );
	}catch(e){
		if( GTDDEBUGMODE ) alert("labelselectui refresh\n" + e.toString());
	}
};
LabelSelectUI.prototype.refresh_addElLabels = function(t, elLabels){
	var ss = this.gtdinboxPage.settings.labels.types["Statuses"];
	
	var fProcessElLabels = GIPROTOTYPE.bind(this, function(elContainer, f){
		var els = elContainer.getElementsByTagName("*");
		for( var i = 0; i < els.length; i++ ){
			if( labelname = els[i].getAttribute("gtdLabelName") ){
				if( !this._elLabelHash[labelname] ) this._elLabelHash[labelname] = [];
				f(labelname, els[i]);
			}
		}
	});
	
	// Remove old cached elements:
	fProcessElLabels(this._elContainers[t], GIPROTOTYPE.bind(this, function(labelname, el){
			for( var i = 0; i < this._elLabelHash[labelname].length; i++ ){
				if( this._elLabelHash[labelname][i]==el ){
					this._elLabelHash[labelname].splice(i,1);
					break;
				}
			}
		}));
	this._elContainers[t].innerHTML = "";
		
	
	// Create new entry:
	fProcessElLabels(elLabels, GIPROTOTYPE.bind(this, function(labelname, el){
			this._elLabelHash[labelname].push(el);
			//if( this._threadActions && t=="Statuses" && labelname==(ss.prefix + ss.defaults.Finished) ){
			//	el.innerHTML = "";
			//	el.appendChild( this._threadActions.getFinishedButton() );
			//}
		}));
		
	var showHider = new Showhider(this.gtdinboxPage, "label-" + t, {doc:this.doc, type:"attach", el:elLabels, titleClass:"title", show:true});
	elLabels.style.display = (showHider.elContent.innerHTML.length==0)? "none" : "";
	this._elContainers[t].appendChild( elLabels );
	this._showHiders.push( showHider );
	
	if( t=="Statuses" && this._threadActions ){
		showHider.elContent.appendChild( this._threadActions.getFinishedButton() );
	}
};

LabelSelectUI.prototype.clickLabel = function(event){
	// Broadcast to subscribers the label that was clicked
try{
	if( event.button!=0 ) return;
	var el = GIPROTOTYPE.element(event);
	
	/*
	if( el.getAttribute("name")=="status-finish" ){	// Remove all status labels active
		var statusPrefix = this.gtdinboxPage.settings.labels.types["Statuses"].prefix;
		for( l in this._labelActiveHash ){
			if( l.indexOf(statusPrefix)==0 ){
				var el = this._elLabelHash[l][0];
				this.eventHandlers.broadcast("click-label", {labelname:l, 'el':el, name:el.getAttribute("gtdName")});
			}
		}
	}else{
	*/
		var label = {name:el.getAttribute("gtdName"), labelname:el.getAttribute("gtdLabelName"), 'el':el}
		if( !label.labelname ) return;
		this.eventHandlers.broadcast("click-label", label);	
	//}
}catch(e){
	if( GTDDEBUGMODE ) alert("labelselectui clickLabel\n" + e.toString());
}
};

LabelSelectUI.prototype.selected = function(labels){

try{
	
	if( typeof labels=="undefined" ){
		var clabels = [];
		for( label in this._labelActiveHash ) clabels.push(label);
		return clabels;
	}
	if( typeof labels=="string" ){
		labels = [labels];
	}
	
	var cHash = {};
	for( label in this._labelActiveHash ) cHash[label] = true;
	
	for( var i = 0; i < labels.length; i++ ){
		if( cHash[labels[i]] ){	// exists, strip it from known hash
			delete cHash[labels[i]];
		}
		// Create (or refresh existing - because in 2d array)
		var elLabels = this._elLabelHash[labels[i]];
		if( elLabels ){
			for( var j = 0; j < elLabels.length; j++ ){
				GIPROTOTYPE.addClassName( elLabels[j], "selected" );
			}
			this._labelActiveHash[labels[i]] = true;
		}
	}
	// Everything remaining in cHash is to be removed
	for( l in cHash ){
		if( this._labelActiveHash[l] ){
			var elLabels = this._elLabelHash[l];
			if( elLabels ){
				for( var j = 0; j < elLabels.length; j++ ){
					GIPROTOTYPE.removeClassName( elLabels[j], "selected" );
				}
			}
			delete this._labelActiveHash[l];
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("labelsui selected\n" + e.toString());
}


};

LabelSelectUI.prototype.newLabels = function(labels){
	if( labels && labels.length>0 ){
		GIPROTOTYPE.uniqueise(labels);
		var html = [];
		html.push("New Labels:");
		for( var i = 0; i < labels.length; i++ ){
			if( i>0 ) html.push(", ");
			html.push(labels[i]);
		}
		this.elNew.innerHTML = html.join("");
		this.elNew.style.display = "";
	}else{
		this.elNew.style.display = "none";
	}
};

LabelSelectUI.prototype.clickCreate = function(event){
	if( this._createWithAjax ){
		this._labels.createLabelAjax();
	}else{
		this._labels.createLabel();
	}
};

LabelSelectUI.prototype.destroy = function(){
	this.eventObservers.destroy();
	this.eventHandlers.destroy();
	
	this.gtdinboxPage = null;
	this.doc = null;
	this.settings = null;
	this._labels = null;
	
	for( var i = 0; i < this._showHiders.length; i++ ){
		this._showHiders[i].destroy();
	}
	
	this._elLabelHash = null;
	this._labelActiveHash = null;
	this._elContainers = null;
	this._types = null;
};
